package com.cbb.net;

import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.cbb.bean.ResponseBase;


/**
 * Created by jxk on 2018/3/5.
 * Email:676585325@qq.com
 */
public abstract class ResponseCallback<E> implements Response.ErrorListener, Response.Listener<E> {
    private static final String TAG = ResponseCallback.class.getSimpleName();
    private Context context;

    public ResponseCallback(Context context) {
        this.context = context;
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {

    }

    @Override
    public void onResponse(E result) {
        if (result == null) {
            onReuquestFailed(new ErrorBase(-1, "没有数据返回"));
            return;
        }
        if (result instanceof ResponseBase) {
            //处理用户相关返回体
            ResponseBase userResultBase = (ResponseBase) result;
            if (userResultBase.status == 0) {
                onRequestSuccess(result);
            }else {
                onReuquestFailed(new ErrorBase(userResultBase.status, userResultBase.msg));
                Logger.d(TAG + " ERROR", userResultBase.msg + "-----" + userResultBase.status);
            }
        }
    }
    public abstract void onRequestSuccess(E result);
    public abstract void onReuquestFailed(ErrorBase error);
}
